import axios from 'axios';
import qs from 'qs'
import store from './store'
import router from './router'
import {
    Toast
} from 'vant';
// http request 拦截器
axios.defaults.timeout = 15000;
axios.defaults.baseURL = 'https://dev.qolayapp.cn'
axios.interceptors.request.use(
    config => {
        store.commit('SHOW_LOADING')
        let userInfo = window.localStorage.getItem('userInfo');
        if (config.method == 'post') {
            config.data = qs.stringify(config.data);
        }
        try {
            if (userInfo) { // 判断是否存在token，如果存在的话，则每个http header都加上token
                userInfo = JSON.parse(userInfo);
                let uid = userInfo.uid;
                config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
                config.url = config.url.includes('?') ? config.url + `&uid=${Number(uid)}` : config.url + `?uid=${Number(uid)}`;
                return config;
            }
        } catch (error) {
            window.location.reload()
        }
        return config;
    },
    err => {
        store.commit('HIDE_LOADING')
        return Promise.reject(err);
    });
axios.interceptors.response.use(res => {
    store.commit('HIDE_LOADING');
    if (res.data.code == 0) {
        if (res.data.data.code == '001') {
            return res.data.data.data
        } else {
            Promise.reject('توردا مەسىلە كۆرۈلدى')
        }
    } else {
        Promise.reject('توردا مەسىلە كۆرۈلدى')
    }
}, err => {
    store.commit('HIDE_LOADING');
    Toast('توردا مەسىلە كۆرۈلدى')
    return Promise.reject(err)
})
let env = 'pro';
let CONF = {
    appid: env == 'dev' ? 'wx216eb31124fe4a25' : 'wx13892ce43308c20d',
    appsecret: env == 'dev' ? '68dad71b23bee9bc6802ceeb02a9578c' : '16abe69eb28c8a11a145ddc7b67d20c4'
}
export {
    CONF as config,
    axios,
    encrypt,
    decrypt
}

function encrypt(message) {
    return CryptoJS.TripleDES.encrypt(
        message,
        "YusupTurak"
    ).toString();
};
// TripleDES 对称 加密
function decrypt(message) {

    return CryptoJS.TripleDES.decrypt(
        message,
        "YusupTurak"
    ).toString(CryptoJS.enc.Utf8);
};